package com.jumi.microservice.config;

import com.jumi.microservice.jwt.ignore.IgnoreProperties;
import com.jumi.microservice.common.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;


@Configuration
@DependsOn("ignorePostProcess")
public class RedisInit {
    @Autowired
    RedisCache redisCache;

    @PostConstruct
    public void init(){
        initIgnoreUrl();
    }
    public void initIgnoreUrl(){
        System.err.println("启动添加忽略接口至Redis----------");
        List<String> ignoreUrls = IgnoreProperties.ignoreUrls;//本项目

        List<Object> cacheList = redisCache.getCacheList(IgnoreProperties.REDIS_IGNORE_KEY);//redis中所有的白名单
        List<Object>  newIgnoreUrls=new ArrayList<>();
        for (String ignoreUrl : ignoreUrls) {
            if(!cacheList.contains(ignoreUrl)){
                newIgnoreUrls.add(ignoreUrl);
            }
        }
        if(newIgnoreUrls.size()>0){
            redisCache.setCacheList(IgnoreProperties.REDIS_IGNORE_KEY,newIgnoreUrls);
        }
    }
}
